﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "../rmutils.h"

MainWindow* MainWindow::g_MainWin=NULL;
QString MainWindow::serverip="";
QString MainWindow::userphone="";
QString MainWindow::groomid="";
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowTitle(QString::fromLocal8Bit("清华同方云视频"));

    connect(&m_webSocket, &QWebSocket::connected, this,[=](){
        qDebug()<<"Socket Connect Success";
        if(m_loginDialg->isVisible())
        {
            m_loginDialg->login();
        }
    });
    connect(&m_webSocket, &QWebSocket::disconnected, this,[=](){
        qDebug()<<"Socket Connect Error";
    });
    connect(&m_webSocket, &QWebSocket::textMessageReceived,
            this, &MainWindow::onTextMessageReceived);

    m_loginDialg = new rm_LoginDialog(this);
    m_loginDialg->show();
    g_MainWin=this;

    m_mainwindow = new rm_MainWindow(this);
    m_mainwindow->hide();

    m_meetingroom = new RM_MeetingRoom(this);
    m_meetingroom->hide();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::enterroom(QString roomid)
{
    QJsonObject enterobj;
    enterobj.insert("type","Type_Enterroom");
    enterobj.insert("room",roomid);
    MainWindow::groomid=roomid;

    m_webSocket.sendTextMessage(RMUtils::JsonToQstring(enterobj));
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    m_loginDialg->resize(this->size());
    m_mainwindow->resize(this->size());
    m_meetingroom->resize(this->size());
}

void MainWindow::leaveroom()
{
    m_loginDialg->hide();
    m_mainwindow->show();
    m_meetingroom->hide();
}

void MainWindow::onTextMessageReceived(QString message)
{
    qDebug() << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Message received:" << message;

    QJsonObject resobj = RMUtils::QstringToJson(message);
    //if(resobj.contains("res"))
    {
        QString restype = resobj.value("type").toString();
        if(restype == "Type_Login")
        {
            if(resobj.value("res").toString() == "success")
            {
                m_loginDialg->hide();
                m_mainwindow->show();
            }
        }
        if(restype == "Type_Leaveroom")
        {
            qDebug()<<"Type_Leaveroom Room Update";
            {

                QString leavephone = resobj.value("phone").toString();
                Tempuserlist.removeOne(leavephone);
                m_meetingroom->updateLoadVideos(Tempuserlist);

            }
        }


        if(restype == "Type_Enterroom")
        {
            qDebug()<<"Enter Room Update";
            {
                m_loginDialg->hide();
                m_mainwindow->hide();
                m_meetingroom->show();
                QString url=QString("video://")+serverip+QString("/")+userphone;
                m_meetingroom->startPush(url);
                QString userlist = resobj.value("phones").toString();
                Tempuserlist = userlist.split(",");
                m_meetingroom->updateLoadVideos(Tempuserlist);

            }
        }
    }

}
